package com.xh.bussiness.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.course.bean.request.CourseChapterRequest;
import com.xh.bussiness.course.dao.CourseChapterMapper;
import com.xh.bussiness.course.model.CourseChapter;
import com.xh.bussiness.course.service.ICourseChapterService;
import com.xh.bussiness.resource.bean.dto.MaterialDto;
import com.xh.bussiness.resource.bean.request.MaterialCourseIndexRequest;
import com.xh.bussiness.resource.constant.MaterialType;
import com.xh.bussiness.resource.model.MaterialChapter;
import com.xh.bussiness.resource.model.MaterialCourseIndex;
import com.xh.bussiness.resource.service.IMaterialChapterService;
import com.xh.bussiness.resource.service.IMaterialCourseIndexService;
import com.xh.core.service.impl.BaseServiceImpl;
import com.xh.core.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class CourseChapterServiceImpl extends BaseServiceImpl<CourseChapterMapper, CourseChapter> implements ICourseChapterService {

    @Autowired
    IMaterialChapterService materialChapterService;
    @Autowired
    IMaterialCourseIndexService materialCourseIndexService;

    @Override
    public List<CourseChapter> queryAll(CourseChapterRequest request) {
//        QueryWrapper<CourseChapter> queryWrapper = new QueryWrapper<>();
//        if(!StringUtils.isEmpty(request.getChapterId())){
//            queryWrapper.eq("chapter_id",request.getChapterId());
//        }
//        if(!StringUtils.isEmpty(request.getCourseIndexId())){
//            queryWrapper.eq("course_index_id",request.getCourseIndexId());
//        }
//        return this.dao.selectList(queryWrapper);
        return this.dao.queryAll(request);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean add(CourseChapterRequest request) {
        QueryWrapper<CourseChapter> queryWrapper = new QueryWrapper<>();
        String courseIndexId = request.getCourseIndexId();
        queryWrapper.eq("course_index_id",courseIndexId);
        this.dao.delete(queryWrapper);

        List<CourseChapter> addList =new ArrayList<>();
        for (int i = 0; i < request.getChapterIds().size(); i++) {
            String chapter = request.getChapterIds().get(i);
            CourseChapter  courseChapter = new CourseChapter();
            courseChapter.setChapterId(chapter);
            courseChapter.setCourseIndexId(courseIndexId);
            addList.add(courseChapter);

            //根据章节查询资料、绑定到课次
            QueryWrapper<MaterialChapter> chapterQueryWrapper = new QueryWrapper<>();
            chapterQueryWrapper.eq("chapter_id",chapter);
            List<MaterialChapter> materialChapters = materialChapterService.selectList(chapterQueryWrapper, MaterialChapter.class);
            Set<String> materialIds = materialChapters.stream().map(MaterialChapter::getMaterialId).collect(Collectors.toSet());

            if(materialIds.size() > 0){

                //删除课次下面的资料
//                MaterialCourseIndexRequest materialCourseIndexRequest =new MaterialCourseIndexRequest();
//                materialCourseIndexRequest.setCourseIndexId(courseIndexId);
//                materialCourseIndexService.delete(materialCourseIndexRequest);
                QueryWrapper<MaterialCourseIndex> materialCourseIndexQueryWrapper =new QueryWrapper<>();
                materialCourseIndexQueryWrapper.eq("course_index_id",courseIndexId);
                materialCourseIndexQueryWrapper.in("material_id",materialIds);
                materialCourseIndexService.deleteByCondition(materialCourseIndexQueryWrapper);

                //重新添加资料绑定
                List<MaterialCourseIndex> materialCourseIndexList = new ArrayList<>();
                for (String materialId : materialIds) {
                    MaterialCourseIndex materialCourseIndex = new MaterialCourseIndex();
                    materialCourseIndex.setCourseIndexId(courseIndexId);
                    materialCourseIndex.setMaterialId(materialId);
                    materialCourseIndexList.add(materialCourseIndex);
                }
                materialCourseIndexService.batchInsert(materialCourseIndexList);
            }



        }

        return this.batchInsert(addList);

    }

    @Override
    public Map<String, Long> queryMaterialCountByType(String userId, String courseIndexId) {
        Map<String,Long> result = new HashMap<>();
        result.put(MaterialType.COURSEWARE,0L);
        result.put(MaterialType.HANDOUT,0L);
        result.put(MaterialType.MEDIA,0L);
        result.put(MaterialType.OTHER,0L);
        result.put(MaterialType.TEACHPLAN,0L);
        result.put(MaterialType.MULTIMEDIA,0L);
        List<Map<String,Object>> maps = this.dao.queryMaterialCountByType(userId, courseIndexId);
        Long totalCount = 0L;
        for(Map<String,Object> map : maps){
            Integer type = (Integer) map.get("type");
            Long count = (Long) map.get("count");
            if(type == null){
                continue;
            }
            totalCount += count;
            if(type == 1){
                result.put(MaterialType.COURSEWARE,count);
            }else if(type == 2){
                result.put(MaterialType.HANDOUT,count);
            }else if(type == 3){
                result.put(MaterialType.MEDIA,count);
            }else if(type == 4){
                result.put(MaterialType.OTHER,count);
            }else if(type == 5){
                result.put(MaterialType.TEACHPLAN,count);
            }else if(type == 6){
                result.put(MaterialType.MULTIMEDIA,count);
            }
        }
        result.put(MaterialType.ALL,totalCount);
        return result;
    }

    @Override
    public List<MaterialDto> queryMaterialByCourseIndex(String userId, String courseIndexId, Integer type) {
        return this.dao.queryMaterialByCourseIndex(userId, courseIndexId, type);
    }

    @Override
    public boolean deleteByCourseIndexId(String courseIndexId,String chapterId) {
        return this.dao.deleteByCourseIndexId(courseIndexId, chapterId);
    }

}
